package com.fishery.refrigeratory.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fishery.entity.GlobalException;
import com.fishery.entity.StatusCode;
import com.fishery.refrigeratory.entity.Refrigeratory;
import com.fishery.refrigeratory.entity.RefrigeratoryInInfo;
import com.fishery.refrigeratory.entity.RefrigeratoryPrepareEnter;
import com.fishery.refrigeratory.entity.vo.RefrigeratoryPrepareEnterVo;
import com.fishery.refrigeratory.mapper.RefrigeratoryPrepareEnterMapper;
import com.fishery.refrigeratory.service.RefrigeratoryInInfoService;
import com.fishery.refrigeratory.service.RefrigeratoryPrepareEnterService;
import com.fishery.refrigeratory.service.RefrigeratoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class RefrigeratoryPrepareEnterImpl extends ServiceImpl<RefrigeratoryPrepareEnterMapper, RefrigeratoryPrepareEnter> implements RefrigeratoryPrepareEnterService {

    @Autowired
    private RefrigeratoryPrepareEnterMapper refrigeratoryEnterMapper;

    @Autowired
    private RefrigeratoryInInfoService refrigeratoryInInfoService;

    @Autowired
    private RefrigeratoryService refrigeratoryService;

    /**
     * 通过产品名称查看待入库信息
     * @return
     */
    @Override
    public List<RefrigeratoryPrepareEnter> getRefrigeratoryEnterByName(String produceName) {
        QueryWrapper<RefrigeratoryPrepareEnter> wrapper = new QueryWrapper<>();
        wrapper.like("product_name", produceName);
        return refrigeratoryEnterMapper.selectList(wrapper);
    }

    //根据产品id获取所有待入库的基地名称
    @Override
    public String getRefrigeratoryEnterByProductId(String id) {
        QueryWrapper<RefrigeratoryPrepareEnter> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id", id);
        List<RefrigeratoryPrepareEnter> refrigeratoryPrepareEnters = refrigeratoryEnterMapper.selectList(wrapper);
        if (refrigeratoryPrepareEnters.isEmpty()) {
            throw new GlobalException(StatusCode.ERROR, "找不到该产品id对应的基地名！请输入合法数据");
        }
        return refrigeratoryPrepareEnters.get(0).getSuppliersSource();
    }

    /**
     * 分页查看所有待入库信息
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<RefrigeratoryPrepareEnter> getPageAllRefrigeratoryEnter(Integer page, Integer size) {
        Page<RefrigeratoryPrepareEnter> refrigeratoryPrepareEnterPage = new Page<>(page, size);
        refrigeratoryEnterMapper.selectPage(refrigeratoryPrepareEnterPage,null);
        return refrigeratoryPrepareEnterPage;
    }

    /**
     * 指定待入库数量部分加入到其对应冷库
     * @param refrigeratoryPrepareEnterVo
     */
    @Override
    @Transactional
    public int enterRefrigeratory(RefrigeratoryPrepareEnterVo refrigeratoryPrepareEnterVo) {
        RefrigeratoryPrepareEnter refrigeratoryPrepareEnter = refrigeratoryEnterMapper.selectById(refrigeratoryPrepareEnterVo.getId());
        if (refrigeratoryPrepareEnter == null) {
            throw new GlobalException(StatusCode.ERROR, "该待入库信息已经不存在！");
        }
        //要修改的重量
        Double productWeight = refrigeratoryPrepareEnter.getProductWeight();
        if (refrigeratoryPrepareEnterVo.getProductWeight() < 0 || productWeight < refrigeratoryPrepareEnterVo.getProductWeight()) {
            throw new GlobalException(StatusCode.ERROR, "输入的容量超出合法范围！");
        }
        Refrigeratory refrigeratory = refrigeratoryService.getRefrigeratory(refrigeratoryPrepareEnter.getBelongRefrigeratoryId());
        if (refrigeratory == null) {
            throw new GlobalException(StatusCode.ERROR, "改冷库信息已经不存在！");
        }
        if (productWeight + refrigeratory.getRefrigeratoryUsedCapacity() > refrigeratory.getRefrigeratoryCapacity()) {
            throw new GlobalException(StatusCode.ERROR, "该冷库已经爆满！无法添加新的容量");
        }
        RefrigeratoryInInfo refrigeratoryInInfo = new RefrigeratoryInInfo();
        refrigeratoryInInfo.setRefrigeratoryId(refrigeratoryPrepareEnter.getBelongRefrigeratoryId());
        refrigeratoryInInfo.setProductName(refrigeratoryPrepareEnter.getProductName());
        refrigeratoryInInfo.setRefrigeratoryInTime(new Date());
        refrigeratoryInInfo.setRefrigeratoryInDescription("添加入库信息！");
        refrigeratoryInInfo.setRefrigeratoryInCapacity(refrigeratoryPrepareEnterVo.getProductWeight());
        refrigeratoryInInfo.setRefrigeratoryInUsedCapacity((double)0);
        refrigeratoryInInfo.setWarehousingPersonId(refrigeratoryPrepareEnterVo.getWarehousingPerson());
        refrigeratoryInInfo.setProcessingBaseId(refrigeratoryPrepareEnter.getProductId());
        int count = refrigeratoryInInfoService.insertInInfo(refrigeratoryInInfo);
        RefrigeratoryPrepareEnter updateEnter = new RefrigeratoryPrepareEnter();
        updateEnter.setId(refrigeratoryPrepareEnterVo.getId());
        updateEnter.setProductWeight(refrigeratoryPrepareEnter.getProductWeight() - refrigeratoryPrepareEnterVo.getProductWeight());
        int count2 = refrigeratoryEnterMapper.updateById(updateEnter);
        if (count <= 0 || count2 <= 0) {
            throw new GlobalException(StatusCode.ERROR, "添加进冷库失败！请刷新重试");
        }
        return count2;
    }
}
